﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 水露点
    /// </summary>
    /// 创建时间：2022-12-23 13:59:50。作者：王浩力
    public partial class PageWaterPoint : Page
    {
        public PageWaterPoint()
        {
            InitializeComponent();
        }
        WaterPointBusiness waterPointBusiness = new WaterPointBusiness();


        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //步骤查询条件  //创建时间：2023-1-17 16:43:23
            cmb_Step.ItemsSource = CommonConfig.StepList;
            cmb_Step.DisplayMemberPath = "Value";
            cmb_Step.SelectedValuePath = "Key";

            LoadDataGrid(1);
        }

        private void LoadDataGrid(int p)
        {
            Water_point water_Point = new Water_point();
            water_Point.Report_no = txtReportNo.Text;
            water_Point.E3 = date1.Text;//开始时间
            water_Point.E4 = date1.Text;//结束时间
            if (cmb_Step.SelectedValue != null && cmb_Step.SelectedValue.ToMyInt64T2() != -1)
            {
                water_Point.StepCurrent = Convert.ToByte(cmb_Step.SelectedValue);
            }

            var page = waterPointBusiness.Page(water_Point, p, CommonConfig.PageSize);
            dataGrid.DataContext = page.List;

            pager.Total = page.Total;
            pager.PageIndex = page.PageIndex;
            pager.PageSize = page.PageSize;
        }

        //编辑
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Water_point;
            WindowWaterPointEdit pointEdit = new WindowWaterPointEdit();
            pointEdit.DataGridRefresh += WindowWaterPointAdd_DataGridRefresh;
            pointEdit.editInfo = info;
            pointEdit.ShowDialog();
        }

        //打印
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            var info = this.dataGrid.SelectedItem as Water_point;

            //读取文本实体
            var infoView = waterPointBusiness.GetView(info.Id);

            //打印备注
            SourceDictBusiness sourceDictBusiness=new SourceDictBusiness();
            infoView.source_Dicts = sourceDictBusiness.GetListByGroup("water_bz_remark_list", 1);

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentWaterPoint.xaml", infoView, new PrintGas_analysisDocument());

            ppv.ShowInTaskbar = false;
            ppv.Show();
        }

        //日志
        private void log_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Water_point;

            WindowWaterpointStepLog log = new WindowWaterpointStepLog();
            log.waterpointId = info.Id;
            log.ShowDialog();
        }

        //校对
        private void proo_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Water_point;
            WindowWaterPointEdit pointEdit = new WindowWaterPointEdit();
            pointEdit.DataGridRefresh += WindowWaterPointAdd_DataGridRefresh;
            pointEdit.editInfo = info;
            pointEdit.Tag = 778;
            pointEdit.ShowDialog();
        }

        //审核
        private void check_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Water_point;

            WindowWaterpointCheck check = new WindowWaterpointCheck();
            check.DataGridRefresh += WindowWaterPointAdd_DataGridRefresh;
            check.editInfo = info;
            check.ShowDialog();
        }

        //删除
        private void del_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Water_point;
            var resultDel = MessageBox.Show("确认删除\"" + info.Report_no + "\"吗？", "删除提示", MessageBoxButton.YesNo);
            if (resultDel != MessageBoxResult.Yes)
            {
                e.Handled = true;
                return;
            }

            var result = waterPointBusiness.Delete(info.Id);
            ShowAlertHelpter.ShowAlert(result.Msg);
            if (result.Code == (int)ResultCode.Success)
            {
                LoadDataGrid(pager.PageIndex);
            }
        }

        //查询
        private void search_Click(object sender, RoutedEventArgs e)
        {
            LoadDataGrid(1);
        }

        //录入
        private void input_Click(object sender, RoutedEventArgs e)
        {
            WindowWaterPointAdd windowWaterPointAdd = new WindowWaterPointAdd();
            windowWaterPointAdd.DataGridRefresh += WindowWaterPointAdd_DataGridRefresh;
            windowWaterPointAdd.ShowDialog();
        }

        private void WindowWaterPointAdd_DataGridRefresh(object? sender, EventArgs e)
        {
            LoadDataGrid(pager.PageIndex);
        }

        private void PageBar_PageIndexChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
        {
            LoadDataGrid(pager.PageIndex);
        }

        //导出 //创建时间：2023-1-3 22:38:46。作者：王浩力
        private void Export_Click(object sender, RoutedEventArgs e)
        {
            btnExport.IsEnabled = false;
            btnExport.Content = "处理中...";

            Water_point water_Point = new Water_point();
            water_Point.Report_no = txtReportNo.Text.Trim();
            water_Point.E3 = date1.Text;//开始时间
            water_Point.E4 = date1.Text;//结束时间

            Task.Run(() =>
            {
                var result = waterPointBusiness.ExportExcel(water_Point);
                this.Dispatcher.InvokeAsync(() =>
                {
                    ShowAlertHelpter.ShowAlert(result.Msg);
                    btnExport.IsEnabled = true;
                    btnExport.Content = "导出";
                });
                if (result.CodeEnum == ResultCode.Success && !result.Msg.Contains("共0条"))
                {
                    if (result.Data != null)
                    {
                        System.Diagnostics.Process.Start("explorer.exe", result.Data.ToString());
                    }
                }
            });
        }
    }
}
